package cn.crex1.xiaomi.core.orm.mapper;

import java.lang.reflect.Field;
import java.lang.reflect.Proxy;

import cn.crex1.xiaomi.core.orm.datasource.JdbcDataSource;

public class MapperFactory {

	private  Class<?> mapperInterface;
	
	public MapperFactory() {
	}
	
	/**
	 *创建代理对象
	 * @param mapperProxy
	 * @return 
	 */
	@SuppressWarnings("unchecked")
	protected <T> T newinstance(MapperProxy mapperProxy) {
		return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);	
	}
	
	/**
	 * 获取动态代理接口对象
	 * @param dataSource
	 * @param clazz
	 * @param args
	 * @return
	 * @throws NoSuchFieldException
	 */
	protected <T> T newinstance(JdbcDataSource dataSource,Class<?> clazz,Object...args) throws NoSuchFieldException {
		MapperProxy mapperProxy =new MapperProxy(dataSource,clazz,args); 
		this.mapperInterface=clazz;
		return newinstance(mapperProxy);
	}
}
